package managedbeans;

import entities.Authorisation;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import langresources.ResourcesController;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import utils.dataaccess.DBInterface;
import utils.dataaccess.IOUtil;
import utils.ValidationManager;
import utils.security.SHAHashUtil;


@ManagedBean(name = "registrationBean")
@SessionScoped
public class RegistrationBean extends GenericForumBean{

    @EJB SHAHashUtil hasher;
    
    private String username;
    private String password;
    private String acceptPassword;
    private String email;
    private int    ID;
    private int    GID;
    private byte[] avatarBytes = null;

    public RegistrationBean() {
    }
    
    public String createAccount(){
        if(ValidationManager.isRegisteredUser(username)){
            showMessage(ResourcesController.extractResource("MESSAGE.WARNING"), ResourcesController.extractResource("USER_EXISTS"));
            return "REGISTRATION_FAILED";
        } else if (!ValidationManager.passwordAccepted(password, acceptPassword)) {
            showMessage(ResourcesController.extractResource("MESSAGE.WARNING"), ResourcesController.extractResource("PASSWORD_NOT_ACCEPTED"));
            return "REGISTRATION_FAILED";
        } else if (!ValidationManager.isEmail(email)) {
            showMessage(ResourcesController.extractResource("MESSAGE.WARNING"), ResourcesController.extractResource("EMAIL_INVALID"));
            return "REGISTRATION_FAILED";
        } else {
            Authorisation authEntry = new Authorisation();
            authEntry.setId(generateID());
            authEntry.setGid(3);
            authEntry.setUsername(username);
            authEntry.setPassword(hasher.getHash(password));
            authEntry.setEmail(email);
            DBInterface.save(authEntry);
            IOUtil.saveAvatar(avatarBytes, username);
            showMessage(ResourcesController.extractResource("MESSAGE.INFO"), ResourcesController.extractResource("ACCOUNT_CREATED"));
            return "REGISTRATION_SUCCEED";
        }
    }
    
    

    public void handleFileUpload(FileUploadEvent evt){
            UploadedFile uploadedFile = evt.getFile();
            avatarBytes = uploadedFile.getContents();
    }


    // GETTERS-SETTERS
    public int getGID() {
        return GID;
    }

    public void setGID(int GID) {
        this.GID = GID;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getAcceptPassword() {
        return acceptPassword;
    }

    public void setAcceptPassword(String acceptPassword) {
        this.acceptPassword = acceptPassword;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
}
